package com.zhkj.txg.module.product.adapter

import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.txg.R
import com.zhkj.txg.module.product.entity.CommentListEntity
import com.zhkj.txg.utils.ImageUtils

class CommentListAdapter : BaseQuickAdapter<CommentListEntity, CommentViewHolder>(R.layout.item_comment_list),
    LoadMoreModule {

    override fun convert(helper: CommentViewHolder, item: CommentListEntity) {
        val imageView = helper.setText(R.id.tvCommentName, item.nickname)
            .setText(R.id.tvCommentDate, item.createTime)
            .setText(
                R.id.tvCommentSpec,
                "规格:${if (item.specValue.isEmpty()) context.getString(R.string.tag_default) else item.specValue}"
            )
            .setText(R.id.tvCommentStatus, item.rank)
            .setText(R.id.tvCommentDesc, item.content)
            .setGone(R.id.rvComment, item.images.isEmpty())
            .getView<ImageView>(R.id.ivComment)
        ImageUtils.loadCircleImage(context, imageView, item.avatar)
        // 设置文字颜色
        when (item.rankLevel) {
            1 -> helper.setTextColorRes(R.id.tvCommentStatus, R.color.colorMain)
                .setBackgroundResource(R.id.tvCommentStatus, R.drawable.bg_stroke_round_main_main_30)
            2 -> helper.setTextColorRes(R.id.tvCommentStatus, R.color.colorGradientMain)
                .setBackgroundResource(R.id.tvCommentStatus, R.drawable.bg_stroke_round_main_30)
            else -> helper.setTextColorRes(R.id.tvCommentStatus, R.color.colorLightText)
                .setBackgroundResource(R.id.tvCommentStatus, R.drawable.bg_stroke_round_gray_30)
        }
        // 设置Adapter
        if (item.images.isNotEmpty()) helper.pictureAdapter.replaceData(item.images)
    }

    override fun onItemViewHolderCreated(viewHolder: CommentViewHolder, viewType: Int) {
        with(viewHolder.getView<RecyclerView>(R.id.rvComment)) {
            layoutManager = GridLayoutManager(context, 3)
            adapter = viewHolder.pictureAdapter
        }
    }
}

class CommentViewHolder(view: View) : BaseViewHolder(view) {
    val pictureAdapter = CommentPictureAdapter()
}